| Conditions | 12 |
| Total Lines | 77 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like chart.js ➔ init_chart often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | function init_chart(grid_id) { |
||
| 2 | |||
| 3 | const ctx = document.getElementById('chart-' + grid_id); |
||
| 4 | var chart; |
||
| 5 | |||
| 6 | function render_grid() { |
||
| 7 | let group_data = $('#' + grid_id).jqGrid('getGridParam', 'groupingView'), |
||
| 8 | chart_labels = [], |
||
| 9 | chart_data = [], |
||
| 10 | datasets = []; |
||
| 11 | |||
| 12 | group_data.groups.forEach(function(group) { |
||
| 13 | chart_labels.push(group.value.replace(/<\/?[^>]+(>|$)/g, "")); |
||
| 14 | chart_data.push(group.summary[0].v); |
||
| 15 | }); |
||
| 16 | |||
| 17 | var label = ''; |
||
| 18 | $('#' + grid_id).jqGrid('getGridParam', 'colModel').forEach(function(col, index) { |
||
| 19 | if (col.name == 'sum') { |
||
| 20 | label = $('#' + grid_id).jqGrid('getGridParam', 'colNames')[index]; |
||
| 21 | } |
||
| 22 | }); |
||
| 23 | |||
| 24 | datasets.push({ |
||
| 25 | label: label, |
||
| 26 | type: 'bar', |
||
| 27 | data: chart_data, |
||
| 28 | borderWidth: 1 |
||
| 29 | }); |
||
| 30 | |||
| 31 | if (group_data.groupField[0] == 'month' || group_data.groupField[0] == 'year') { |
||
| 32 | let averages = [], |
||
| 33 | periods = Math.min(Math.floor(chart_data.length / 6), 11); |
||
| 34 | chart_data.forEach(function(value, index) { |
||
| 35 | if (index >= periods) { |
||
| 36 | let sum = value; |
||
| 37 | for (let i = 1; i <= periods; i++) { |
||
| 38 | sum += chart_data[index - i]; |
||
| 39 | } |
||
| 40 | averages.push(sum / (periods + 1)); |
||
| 41 | } else { |
||
| 42 | averages.push(null); |
||
| 43 | } |
||
| 44 | }); |
||
| 45 | datasets.push({ |
||
| 46 | label: 'Avg (' + (periods + 1) + ')', |
||
| 47 | type: 'line', |
||
| 48 | tension: .4, |
||
| 49 | pointStyle: false, |
||
| 50 | data: averages |
||
| 51 | }); |
||
| 52 | } |
||
| 53 | |||
| 54 | chart = new Chart(ctx, { |
||
| 55 | data: { |
||
| 56 | labels: chart_labels, |
||
| 57 | datasets: datasets |
||
| 58 | }, |
||
| 59 | options: { |
||
| 60 | scales: { |
||
| 61 | y: { |
||
| 62 | beginAtZero: true |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | }); |
||
| 67 | } |
||
| 68 | |||
| 69 | $('#chgrouping_' + grid_id).on('change', function() { |
||
| 70 | if (chart) { |
||
| 71 | chart.destroy(); |
||
| 72 | } |
||
| 73 | if ($('#' + grid_id).jqGrid('getGridParam', 'grouping')) { |
||
| 74 | render_grid(); |
||
| 75 | } |
||
| 76 | }).trigger('change'); |
||
| 77 | } |